package pt.mauromad.guias.service;

import static java.lang.String.format;

import java.util.Comparator;
import java.util.List;

import org.springframework.stereotype.Service;

import pt.mauromad.guias.dao.AccountDao;
import pt.mauromad.guias.dao.DomainDao;

@Service
public class DomainService extends DaoService<DomainDao, Long> {
	public DomainService() {
		super(DomainDao.class, new Comparator<DomainDao>() {
			@Override
			public int compare(DomainDao o1, DomainDao o2) {
				if (o1.getAccount() == null && o2.getAccount() == null)
					return o1.getDomain().compareTo(o2.getDomain());

				return o1.getAccount() == null ? -1 : 1;
			}
		});
	}

	public List<DomainDao> available() {
		return list(format("where id not in (select domain.id from %s)", AccountDao.class.getName()));
	}

	public DomainDao get(String domain) {
		return one("where domain = :0", domain);
	}
}
